﻿/**
* CRL
*/
using MongoDB.Bson;
using CRL.Data;
using CRL.Data.DBAccess;
using CRL.Data.LambdaQuery;
using CRL.Data.LambdaQuery.CRLExpression;
using CRL.Mongo;
using System;
using testConfig;

namespace MongoTest
{
    class ConfigInit
    {
        public static void Init()
        {
            var builder = DBConfigRegister.GetInstance();
            builder.UseMongoDB();

            builder.RegisterDBAccessBuild(dbLocation =>
            {
                return new DBAccessBuild(DBType.MongoDB, ConnectionStrings.mongoDb);
            });
            //自定义扩展方法
            builder.AddCustomExtendMethod(DBType.MongoDB, nameof(ExtendMethod.isnull), isnull);
            builder.AddCustomExtendMethod(DBType.MongoDB, nameof(ExtendMethod.dateFormatTo), dateFormatTo);
        }
        private static string isnull(MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field = methodInfo.MemberName;
            var args = BsonValue.Create(methodInfo.Args[0]);
            var bs = new BsonDocument("$ifNull", new BsonArray().Add("$" + field).Add(args));
            methodInfo.CustomReturn = bs;
            return "";
        }
        private static string dateFormatTo(MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field = methodInfo.MemberName;
            var args = BsonValue.Create(methodInfo.Args[0]);
            var bs = new BsonDocument("$dateToString", new BsonDocument().Add("format", args).Add("date", $"${field}"));
            methodInfo.CustomReturn = bs;
            return "";
        }
    }
    public static class ExtendMethod
    {
        public static string isnull(this string origin, string v)
        {
            return origin;
        }
        public static string dateFormatTo(this DateTime origin, string format)
        {
            return "";
        }
    }
}
